package com.metamech.vorpal.handler;

import org.apache.log4j.Logger;

import com.metamech.jabber.Session;
import com.metamech.jabber.xml.Packet;
import com.metamech.jabber.xml.PacketListener;
import com.metamech.log.Log;
import com.metamech.vorpal.UserIndex;
/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

public class CloseStreamHandler implements PacketListener {
	private static Logger logger=Logger.getLogger(CloseStreamHandler.class);
	UserIndex userIndex;
	
	public CloseStreamHandler(UserIndex index) { 
		userIndex = index; 
	}

	public void notify(Packet packet){
		try {
			logger.info("Closing session");
			Session session = packet.getSession();
			session.getWriter().write("</stream:stream> ");
			session.getWriter().flush();
			session.getSocket().close();
			userIndex.removeSession(session);
			logger.info("Closed session");
	    } catch (Exception ex){
	    	logger.error("CloseStreamHandler: ",ex);
	    	userIndex.removeSession(packet.getSession());
	    	logger.info("Exception closed session");
	    }
	}
	
	/*SessionIndex sessionIndex;
	public CloseStreamHandler(SessionIndex index) { 
		sessionIndex = index; 
	}

	public void notify(Packet packet){
		try {
			Log.trace("Closing session");
			Session session = packet.getSession();
			GroupChatManager.getManager().removeUser(session.getJID().toString());
			session.getWriter().write("</stream:stream>");
			session.getWriter().flush();
			session.getSocket().close();
			sessionIndex.removeSession(session);
			Log.trace("Closed session");
		} catch (Exception ex){
			Log.error("CloseStreamHandler: ",ex);
			sessionIndex.removeSession(packet.getSession());
			Log.trace("Exception closed session");
		}
  }*/
}